// pages/index/choosehighschool/choosehighschool.js
import {
    GetCleanQue, // 查专业
} from '../../../api/index'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        screenList: [{
                title: "本科",
                choose: 1
            },
            {
                title: "专科",
                choose: 0
            }
        ],
        currentIndex: 1, // 1本科 2专科
        listName: [],
        classfiySelect: "",
        eachRightItemToTop: [], // 右侧每类数据到顶部的距离（用来与 右 => 左 联动时监听右侧滚动到顶部的距离比较）
    },

    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this._GetCleanQue("?majorType=1", true)

    },

    // 确定
    makeAction() {
        wx.navigateTo({
          url: '/pages/index/choosehighschoolresult/choosehighschoolresult',
        })
    },

    // 筛选条件点击事件
    screenCellAction: function (event) {
        console.log(event)
        let itemindex = event.currentTarget.dataset.index;
        this.setData({
            currentIndex:itemindex + 1
        })
        for (let index = 0; index < this.data.screenList.length; index++) {
            if (index == itemindex) {
                this.setData({
                    ['screenList[' + itemindex + '].choose']: 1,
                })
            } else {
                this.setData({
                    ['screenList[' + index + '].choose']: 0,
                })
            }
        }
        this._GetCleanQue("?majorType=" + this.data.currentIndex, true)
    },

    // 专业点击事件
    professionSelectAction: function (event) {
        wx.navigateTo({
          url: '/pages/index/professiondetail/professiondetail?id=' + event.currentTarget.dataset.id + '&educational=' + this.data.currentIndex,
        })
    },

    itemShowSelectAction: function (event) {
        console.log(event)
        let index = event.currentTarget.dataset.index;
        let twoIndex = event.currentTarget.dataset.twoindex;
        let show = this.data.listName[index].twoGradeList[twoIndex].show;
        let array = this.data.listName[index].twoGradeList[twoIndex].threeGradeList;
        let height = array.length * 40;
        let id = this.data.listName[index].id

        console.log(id)
        console.log(array)
        console.log(height)
        console.log(this.data.eachRightItemToTop)
        let {
            eachRightItemToTop
        } = this.data

        if (show == undefined || show == false) {
            console.log('走1')
            // 遍历对象的所有键并设置新值
            for (let key in eachRightItemToTop) {
                if (eachRightItemToTop.hasOwnProperty(key)) {
                    console.log(key)
                    console.log(id)
                    if (key > id || key == 'last') {
                        eachRightItemToTop[key] = eachRightItemToTop[key] + height;
                    }
                }
            }

            this.setData({
                ['listName[' + index + '].twoGradeList[' + twoIndex + '].show']: true,
                eachRightItemToTop
            })
        } else {
            console.log('走2')
            for (let key in eachRightItemToTop) {
                if (eachRightItemToTop.hasOwnProperty(key)) {
                    console.log(key)
                    console.log(id)
                    if (key > id || key == 'last') {
                        eachRightItemToTop[key] = eachRightItemToTop[key] - height;
                    }
                }
            }
            this.setData({
                ['listName[' + index + '].twoGradeList[' + twoIndex + '].show']: false,
                eachRightItemToTop
            })
        }
        console.log(this.data.eachRightItemToTop)
        console.log(this.data.listName[index].twoGradeList[twoIndex].show);

    },


    getEachRightItemToTop: function () { // 获取每个右侧的 bar 到顶部的距离，用来做后面的计算。
        var obj = {};
        var totop = 0;
        obj[this.data.listName[0].id] = totop // 右侧第一类肯定是到顶部的距离为 0
        for (let i = 1; i < (this.data.listName.length + 1); i++) { // 循环来计算每个子类到顶部的高度
            totop += (40 + 6 + this.data.listName[i - 1].twoGradeList.length * 40)
            obj[this.data.listName[i] ? this.data.listName[i].id : 'last'] = totop // 这个的目的是 例如有两类，最后需要 0-1 1-2 2-3 的数据，所以需要一个不存在的 'last' 项，此项即为第一类加上第二类的高度。
        }
        return obj
    },

    //滚动触发
    scroll: function (e) { // 监听右侧的滚动事件与 eachRightItemToTop 的循环作对比 从而判断当前可视区域为第几类，从而渲染左侧的对应类。

        for (let i = 0; i < this.data.listName.length; i++) {
            let left = this.data.eachRightItemToTop[this.data.listName[i].id]
            let right = this.data.eachRightItemToTop[this.data.listName[i + 1] ? this.data.listName[i + 1].id : 'last']
            // console.log(left)
            // console.log(right)
            if (e.detail.scrollTop < right && e.detail.scrollTop >= left) {
                this.setData({
                    classfiySelect: this.data.listName[i].id,
                    leftToTop: 30 * i
                })
            }
        }
        console.log(this.data.eachRightItemToTop)

        // var scrollTop = e.detail.scrollTop;
        // var h = 0;
        // var classfiySelect;
        // var that = this;

        // that.data.listName.forEach(function (clssfiy, i) {
        //     // 假设这里的 'length' 方法是你自定义的，用于获取每个分类的项目总数
        //     // 如果你只是想获取字符串的长度，应该是 clssfiy['id'].length
        //     var _h = 30 + that.length(clssfiy['id']) * 38;
        //     if (scrollTop >= h && scrollTop < h + _h) { // 确保 scrollTop 在当前分类的高度范围内
        //         classfiySelect = clssfiy['id'];
        //     }
        //     h += _h; // 累加高度
        //     console.log(h)
        // });

        // that.setData({
        //     classfiySelect: classfiySelect,
        // });
    },
    //求每一栏高度
    length: function (e) {
        var that = this;
        for (var i = 0; i < that.data.listName.length; i++) {
            let array = that.data.listName[i].twoGradeList
            for (var j = 0; j < array.length; j++) {
                if (that.data.listName[i]['id'] == e) {
                    return that.data.listName[i]['twoGradeList'].length;
                }
            }
        }
    },
    //点击左边事件
    left_list: function (e) {
        var that = this;
        var l_id = e.currentTarget.dataset.id;
        that.setData({
            rigId: l_id,
            classfiySelect: l_id,
        })
    },
    //跳转详情界面
    particulars: function (e) {

    },

    _GetCleanQue(data, isLoading) {
        console.log(data)
        // console.log(this.data.totalPages)
        // console.log(this.data.listArray.length)
        // let that = this
        // if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        //     this.setData({
        //       nomore: true
        //     })
        //     return;
        //   }
        let that = this
        GetCleanQue(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                this.setData({
                    listName: res.rows,
                    classfiySelect: res.rows[0].id
                })
                this.setData({
                    eachRightItemToTop: that.getEachRightItemToTop()
                })
                console.log(that.getEachRightItemToTop())
            } else {

                Toast(res.msg)
            }
        })
    },



    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})